package com.yatong.service.impl;

import com.yatong.dao.IntentionalCompanyDao;
import com.yatong.entity.Company;
import com.yatong.dao.CompanyDao;
import com.yatong.entity.IntentionalCompany;
import com.yatong.service.CompanyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 公司资料(Company)表服务实现类
 *
 * @author makejava
 * @since 2018-09-21 17:43:25
 */
@Service("companyService")
public class CompanyServiceImpl implements CompanyService {
    @Resource
    private CompanyDao companyDao;
    @Resource
    private IntentionalCompanyDao intentionalCompanyDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Company queryById(Integer id) {
        return this.companyDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Company> queryAllByLimit(int offset, int limit) {
        return this.companyDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param company 实例对象
     * @return 实例对象
     */
    @Override
    public Company insert(Company company) {
        company.setSource("个人");
        company.setPhoneState("0");
        this.companyDao.insert(company);
        return company;
    }

    /**
     * 筛选条件查询多条数据
     *
     * @param company
     * @return
     */
    public List<Company> queryAll(Company company,String tuijian){
        List<Company> companies;
        if (tuijian.equals("true"))
            companies = this.companyDao.queryAlltuijian(company);
        else
            companies = this.companyDao.queryAll(company);
        return companies;
    }

    /**
     * 修改数据
     *
     * @param company 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Company company) {
        if (company.getPhoneState() != null){
            if (company.getPhoneState().equals("4")){
                Company company1 = companyDao.queryById(company.getId());
                IntentionalCompany intentionalCompany = new IntentionalCompany();

                intentionalCompany.setCompanyId(company1.getId());
                intentionalCompany.setCompanyName(company1.getCompanyName());
                intentionalCompany.setAddress(company1.getAddress());
                intentionalCompany.setContacts(company1.getContacts());
                intentionalCompany.setTelePhone(company1.getTelePhone());
                intentionalCompany.setMobilePhone(company1.getMobilePhone());
                intentionalCompany.setType(company1.getType());
                intentionalCompany.setAddTime(new Date());
                intentionalCompany.setCompanyState("0");
                intentionalCompany.setSource(company1.getSource());

                intentionalCompanyDao.insert(intentionalCompany);
            }
        }
        return this.companyDao.update(company);
//        return this.queryById(company.getId());
    }

    /**
     * 修改数据
     *
     * @param company 实例对象
     * @return 实例对象
     */
    @Override
    public int updateCompany(Company company) {
        return this.companyDao.update(company);
//        return this.queryById(company.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.companyDao.deleteById(id) > 0;
    }
}